#include "Student.h"
#include"globalFile.h"
#include "OrderFile.h"
#include<string>
#include<fstream>
#include<vector>
#include<unistd.h>
using namespace std;

    


Student::Student(){};

// 构造函数
Student::Student(std::string m_Account,std::string m_Name, std::string m_Pwd)
{
    this->m_Account=m_Account;
    this->m_Name=m_Name;
    this->m_Pwd=m_Pwd;



};

Student::~Student(){};

// 虚函数
void Student::openMenu()
{
    cout << "欢迎学生代表：" << this->m_Name << "登录！" << endl;
	cout << "\t\t ----------------------------------\n";
	cout << "\t\t|                                 |\n";
	cout << "\t\t|          1.申请预约              |\n";
	cout << "\t\t|                                 |\n";
	cout << "\t\t|          2.查看我的预约           |\n";
	cout << "\t\t|                                 |\n";
	cout << "\t\t|          3.查看所有预约           |\n";
	cout << "\t\t|                                 |\n";
	cout << "\t\t|          4.取消预约              |\n";
	cout << "\t\t|                                 |\n";
	cout << "\t\t|          0.注销登录              |\n";
	cout << "\t\t|                                 |\n";
	cout << "\t\t ----------------------------------\n";
	cout << "请选择您的操作： " << endl;

	// 输入机房容量
	ifstream ifs;
	ifs.open(COMPUERT_FILE,ios::in);

	ComputerRoom c;
	while(ifs>>c.m_RoomID &&ifs>>c.m_MaxCapatity)
	{
		vCom.push_back(c);
	}

	ifs.close();

};

// 申请预约
void Student::applyReservation()
{
	cout << "机房开放时间为周一至周五！" << endl;
	cout << "请输入申请预约的时间：" << endl;
	cout << "1、周一" << endl;
	cout << "2、周二" << endl;
	cout << "3、周三" << endl;
	cout << "4、周四" << endl;
	cout << "5、周五" << endl;

	int date=0;
	int interval=0;
	int room=0;

	while (true)
	{
		cin >> date;
		if (date >= 1 && date <= 5)
		{
			break;
		}
		cout << "输入有误，请重新输入" << endl;
	}


	cout << "请输入申请预约的时间段：" << endl;
	cout << "1、上午" << endl;
	cout << "2、下午" << endl;

	while (true)
	{
		cin >> interval;
		if (interval >= 1 && interval <= 2)
		{
			break;
		}
		cout << "输入有误，请重新输入" << endl;
	}

	cout << "请选择机房：" << endl;
	cout << "1号机房容量：" << vCom[0].m_MaxCapatity << endl;
	cout << "2号机房容量：" << vCom[1].m_MaxCapatity << endl;
	cout << "3号机房容量：" << vCom[2].m_MaxCapatity << endl;

	while (true)
	{
		cin >> room;
		if (room >= 1 && room <= 3)
		{
			break;
		}
		cout << "输入有误，请重新输入" << endl;
	}

	cout << "预约成功！审核中" << endl;

	

	ofstream ofs(ORDER_FILE, ios::app);
	ofs << "date:" << date << " ";
	ofs << "interval:" << interval << " ";
	ofs << "stuId:" << this->m_Account << " ";
	ofs << "stuName:" << this->m_Name << " ";
	ofs << "roomId:" << room << " ";
	ofs << "status:" << 1 << endl;

	ofs.close();

	sleep(2);
	system("clear");



};

// 查看我的预约
void Student::showMyReservation()
{
	OrderFile of;
	if(of.m_size==0)
	{
		cout<<"无预约记录\n";
		sleep(2);
		return;
	}

	bool have_order=false;
	for(int i=0 ;i<of.m_size;i++)
	{
		if(of.m_orderData[i]["stuId"]==this->m_Account)
		{
			cout<<"预约日期：周"<<of.m_orderData[i]["date"]<<"  ";
			cout<<"预约时间段："<<(of.m_orderData[i]["interval"]=="1" ? "上午" : "下午")<<"  ";
			cout<<"机房："<<of.m_orderData[i]["roomId"]<<"  ";
			
			// 0 取消的预约; 1 审核中; 2 已预约; -1 预约失败
			string status="状态：";
			if(of.m_orderData[i]["status"]=="1")
			{
				status+="审核中";
			}
			else if (of.m_orderData[i]["status"]=="2")
			{
				status+="预约成功";
			}
			else if (of.m_orderData[i]["status"]=="-1")
			{
				status+="审核未通过，预约失败";
			}
			else 
			{
				status+="审核已取消";
			}
			cout<<status<<endl;
			have_order=true;
		}
		
		if (!have_order)
		{
			cout<<"该帐号目前没有预约记录\n";
		}

		sleep(5);
		system("clear");

	}


};

// 查看所有预约
void Student::showAllReservation()
{
	OrderFile of;
	if(of.m_size==0)
	{
		cout<<"无预约记录\n";
		sleep(2);
		return;
	}

	for(int i=0 ;i<of.m_size;i++)
	{
		
		cout<<i+1<<". ";
		cout<<"预约日期：周"<<of.m_orderData[i]["date"]<<"  ";
		cout<<"预约时间段："<<(of.m_orderData[i]["interval"]=="1" ? "上午" : "下午")<<"  ";
		cout<<"学号："<<of.m_orderData[i]["stuId"]<<"  ";
		cout<<"姓名："<<of.m_orderData[i]["stuName"]<<"  ";
		cout<<"机房："<<of.m_orderData[i]["roomId"]<<"  ";
		
		// 0 取消的预约; 1 审核中; 2 已预约; -1 预约失败
		string status="状态：";
		if(of.m_orderData[i]["status"]=="1")
		{
			status+="审核中";
		}
		else if (of.m_orderData[i]["status"]=="2")
		{
			status+="预约成功";
		}
		else if (of.m_orderData[i]["status"]=="-1")
		{
			status+="审核未通过，预约失败";
		}
		else 
		{
			status+="审核已取消";
		}
		cout<<status<<endl;
		
	}

	sleep(3);
	system("clear");

	

};

// 取消预约
void Student::cancelReservation()
{
	OrderFile of;
	if(of.m_size==0)
	{
		cout<<"无预约记录\n";
		sleep(2);
		system("clear");
		return ;
	}

	cout<<"审核中和预约成功的记录可以取消，请输入想要取消记录的编号"<<endl;

	vector<int>v;
	int index=1;
	for(int i=0; i<of.m_size;i++)
	{
		if(of.m_orderData[i]["stuId"]==this->m_Account)
		{
			if(of.m_orderData[i]["status"]=="1" ||of.m_orderData[i]["status"]=="1")
			{
				v.push_back(index);
				cout<<index++<<"、";
				cout<<"预约日期：周"<<of.m_orderData[i]["date"]<<"  ";
				cout<<"预约时间段："<<(of.m_orderData[i]["interval"]=="1" ? "上午" : "下午")<<"  ";
				cout<<"机房："<<of.m_orderData[i]["roomId"]<<"  ";

				// 0 取消的预约; 1 审核中; 2 已预约; -1 预约失败
				string status="状态：";
				if(of.m_orderData[i]["status"]=="1")
				{
					status+="审核中";
				}
				else if (of.m_orderData[i]["status"]=="2")
				{
					status+="预约成功";
				}
				cout<<status<<endl;
			}
		}
	}
	cout<<"请输入取消的预约记录， 0代表返回"<<endl;
	int select=0;

	while(true)
	{
		cin>>select;
		if(select>=0 && select<=v.size())
		{
			if (select==0)
				break;
			else
			{
				of.m_orderData[v[select-1]]["status"]="0";
				of.updateOrder();
				cout<<"已取消预约"<<endl;
				break;
			}
		}

		cout<<"输入有误，请重新输入"<<endl;
	}
	sleep(2);
	system("clear");
		

};






